package com.gjy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gjy.dao.ScourseMapper;
import com.gjy.domain.*;
import com.gjy.service.AdminService;
import com.gjy.service.ReportService;
import com.gjy.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    ScourseMapper scourseMapper;

    @Autowired
    StudentService studentService;

    @Autowired
    ReportService reportService;


    @ResponseBody
    @RequestMapping(value = "/deleteReport",method = RequestMethod.POST)
    public Msg deleteReport(Report report){
        reportService.deleteReport(report);
        return Msg.success();
    }

    @ResponseBody
    @RequestMapping(value = "/updateReport",method = RequestMethod.POST)
    public Msg updateReport(Report report){
        reportService.updateReport(report);
        return Msg.success();
    }

    @ResponseBody
    @RequestMapping(value = "/saveReport",method = RequestMethod.POST)
    public Msg saveReport(Report report){
        reportService.add(report);
        return Msg.success();
    }

    @RequestMapping(value = "/delEmp",method = RequestMethod.POST)
    @ResponseBody
    public Msg deleteEmpById(@RequestParam("id")Integer id){
        studentService.deleteById(id);
        return Msg.success();
    }

    @ResponseBody
    @RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
    public Msg updateStudent(Student student){
        studentService.update(student);
        return Msg.success();
    }

    @ResponseBody
    @RequestMapping(value = "/getStudent",method = RequestMethod.POST)
    public Msg getStudent(@RequestParam(value = "id") Integer id){
        Student one = studentService.getOne(id);
        return Msg.success().add("student",one);
    }

    @RequestMapping(value = "/saveStudent",method = RequestMethod.POST)
    @ResponseBody
    public Msg saveStudent(Student student){
        studentService.add(student);
        return Msg.success();
    }

    @RequestMapping(value = "/admLogin",method = RequestMethod.POST)
    public String admLogin(@RequestParam("admId") String name, @RequestParam("admPassword") String password){
        List<Admin> all = adminService.findAll();
        if (!name.isEmpty()){
            int i = Integer.parseInt(name);
            for (Admin admin : all) {
                if (admin.getId().equals(i) && admin.getPassword().equals(password)) {
                    return "admin";
                }
            }
        }
        return "error";
    }

    @RequestMapping(value = "/scourse")
    public String findCourse(@RequestParam("id") Integer id, Model model){
        List<Scourse> all = scourseMapper.findAll(id);
        model.addAttribute("courses",all);
        return "scourse";
    }

    @ResponseBody
    @RequestMapping(value = "/findAll")
    public Msg findAllWithJson(@RequestParam(value = "pn",defaultValue = "1") Integer pn){
        PageHelper.startPage(pn,5);
        List<Student> all = studentService.findAll();
        PageInfo<Student> pageInfo = new PageInfo<Student>(all, 5);
        return Msg.success().add("pageInfo",pageInfo);
    }
}
